package org.jash.shop2201.shop

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dagger.hilt.android.AndroidEntryPoint
import org.jash.shop2201.R
import org.jash.shop2201.databinding.ActivityShopBinding
import org.jash.shop2201.databinding.DialogUpdateBinding
import org.jash.shop2201.mvvm.BaseActivity
import org.jash.shop2201.mvvm.BaseViewModel
import org.jash.shop2201.utils.Event

@Route(path = "/shop/main")
@AndroidEntryPoint
class ShopActivity :  BaseActivity<ActivityShopBinding, BaseViewModel>() {
    lateinit var dialog:Dialog
    override fun initData() {
    }

    override fun initView() {
        binding.navigation.setOnItemSelectedListener {
            supportFragmentManager.beginTransaction()
                .replace(R.id.container, ARouter.getInstance().build(when(it.itemId) {
                    R.id.category -> "/shop/category"
                    R.id.cart -> "/shop/cart"
                    R.id.mine -> "/shop/mine"
                    else -> "/shop/home"
                }).navigation() as Fragment)
                .commit()
            true
        }
        binding.navigation.selectedItemId = R.id.home
        val update = DialogUpdateBinding.inflate(layoutInflater)
        dialog = MaterialAlertDialogBuilder(this)
            .setView(update.root)
            .setCancelable(false)
            .setBackground(ColorDrawable(Color.TRANSPARENT))
            .show()
        update.close.setOnClickListener { dialog.dismiss() }
    }
    @Event("购物车数量")
    fun updateCartSize(size:Int) {
        val badge = binding.navigation.getOrCreateBadge(R.id.cart)
        badge.isVisible = size != 0
        badge.number = size
    }
}